package com.itump.base.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import org.springframework.beans.BeanUtils;

/**
 *  
 * 作者: itump<br>
 * 功能描述: 基础函数<br>
 * 创建时间: 2011-07-15 15:57:25<br>
 * 版本信息: 1.0<br>
 * Copyright: Copyright (c) 2011<br>
 */
public class Utils {
	/**
	 * Constructor
	 */
	private static final String defaultFormat="yyyy-MM-dd";
	private Utils() {
	}

	/**
	 * 功能: 返回编码，位数不够的前面加'0'
	 * 
	 * @param count
	 *            type of int
	 * @param str
	 *            type String
	 * @return String
	 */
	public static String getCodeStr(int count, String str) {
		/*String s = Constants.ZERO_STR + getStr(str);
		return s.substring(s.length() - count);*/
		return null;
	}

	/**
	 * 功能: 返回编码，位数不够的前面加'0'
	 * 
	 * @param count
	 *            type of int,位数
	 * @param prex
	 *            type of String, 前缀
	 * @param str
	 *            type String, id
	 * @return String
	 */
	public static String getCodeStr(int count, String prex, String str) {
		/*String s = Constants.ZERO_STR + getStr(str);
		return (getStr(prex) + s.substring(s.length() - count));*/
		return null;
	}

	/**
	 * 功能: 返回下一个编码，位数不够的前面加'0'
	 * 
	 * @param prex
	 *            type of String
	 * @param str
	 *            type String
	 * @param next
	 *            type of int
	 * @return String
	 */
	public static String getCodeNextStr(String prex, String str, int next) {
		/*String p = getStr(prex);
		String s = getStr(str);
		s = (s.trim().equals("")) ? Constants.ZERO_STR : s;
		int rs = parseInt(s.substring(p.length())) + next;
		int count = s.length() - p.length();
		s = Constants.ZERO_STR + rs;
		s = s.substring(s.length() - count);
		return (p + s);*/
		return null;
	}

	/**
	 * 功能: 返回正确格式的年月字符串,如：200509转化成2005-09
	 * 
	 * @param dt
	 *            type of String
	 * @return String
	 */
	public static String getFormatStr(String dt) {
		String d = getStr(dt).trim();
		if (d.length() > 4) {
			d = d.substring(0, 4) + "-" + d.substring(4);
		}
		return d;
	}
	
	/**
	 * 功能: 把字符串转化成数字
	 * 
	 * @param str
	 *            type of String
	 * @return int
	 */
	public static int parseInt(String str) {
		int rs = 0;
		try {
			rs = Integer.parseInt(str);
		} catch (NumberFormatException e) {
			return 0;
		}
		return rs;
	}
	/**
	 * 功能: 日期转换为制定字符窜
	 * 
	 * @param date
	 *         type of Date
	 * @param format
	 *            type of String
	 * @return int
	 */
	public static String parseDate(Date date,String format) {
		
		DateFormat mat=null;
		try {
			 mat=new SimpleDateFormat(format);
		} catch (NumberFormatException e) {
			return "";
		}
		return mat.format(date);
	}
	/**
	 * 功能: 得到不同格式的系统当前日期
	 * 
	 * @param str
	 *            type of String
	 * @return int
	 */
	public static Date getCurrentDate(String format) {
		
		Date date=new Date();
		Date returnDate=null;;
		if(format==null||"".equals(format))
		{
			format=Utils.defaultFormat;
		}
		DateFormat mat=new SimpleDateFormat(format);
		try {
			returnDate=mat.parse(mat.format(date));
		} catch (Exception e) {
			
		}
		return returnDate;
	}
	/**
	 * 功能: 返回默认格式的系统当前日期,如：2005-09-03
	 * 
	 * 
	 *           
	 * @return Date
	 */
	public static String  getDefaultCurrentDateStr() {
		Date date=new Date();
		String  returnDate=null;;
		DateFormat mat=new SimpleDateFormat(Utils.defaultFormat);
		try {
			returnDate=mat.format(date);
		} catch (Exception e) {
			
		}
		return returnDate;
	}
	
	/**
	 * 功能: 返回默认格式的系统当前日期字符串,如：2005-09-03
	 * 
	 * 
	 *           
	 * @return Date
	 */
	public static Date getDefaultCurrentDate() {
		Date date=new Date();
		Date returnDate=null;;
		DateFormat mat=new SimpleDateFormat(Utils.defaultFormat);
		try {
			returnDate=mat.parse(mat.format(date));
		} catch (Exception e) {
			
		}
		return returnDate;
	}
	/**
	 * 功能: 把Object对象转化成String
	 * 
	 * @param obj
	 *            type of Object
	 * @return String
	 */
	public static String parseStr(Object obj) {
		if (obj == null)
			return "";
		return obj.toString();
	}

	/**
	 * 功能: 把字符串转化成数字
	 * 
	 * @param str
	 *            type of String
	 * @return int
	 */
	public static long parseLong(String str) {
		long rs = 0;
		try {
			rs = Long.parseLong(str);
		} catch (NumberFormatException e) {
			return 0;
		}
		return rs;
	}
	
	/**
	 * 功能: 把字符串转化成数字
	 * 
	 * @param str
	 *            type of String
	 * @return float
	 */
	public static float parseFloat(String str) {
		float rs = 0;
		try {
			rs = Float.parseFloat(str);
		} catch (NumberFormatException e) {
			return 0;
		}
		return rs;
	}

	/**
	 * 功能: 替换字符串
	 * 
	 * @param src
	 * @param oldStr
	 * @param newStr
	 * @return
	 */
	public static String replace(String src, String oldStr, String newStr) {
		int index = src.indexOf(oldStr);
		if (index == -1)
			return src;
		int len = src.length();
		int sublen = oldStr.length();
		return (src.substring(0, index) + newStr + replace(src.substring(index
				+ sublen, len), oldStr, newStr));
	}

	/**
	 * 功能: 把字符串中的“''”替换为“'”
	 * 
	 * @param str
	 * @return
	 */
	public static String replaceQuoted(String str) {
		if (str == null)
			return "";
		if (str.indexOf("'") != -1) {
			str = replace(str, "'", "''");
		}
		return str;
	}

	/**
	 * 功能: 把'\'或是'/'替换成'.'
	 * 
	 * @param str
	 *            type of String, source str
	 * @return String
	 */
	public static String repaceToDot(String str) {
		String rt = str;
		if (rt == null)
			return null;
		int index1 = rt.indexOf("/");
		int index2 = rt.indexOf("\\");
		if ((index1 == -1) && (index2 == -1))
			return rt;
		rt = rt.replace('/', '.');
		rt = rt.replace('\\', '.');

		return rt;
	}

	/**
	 * 功能: 得到一个模糊匹配的语句, 添加“'”，和“%”
	 * 
	 * @param str
	 *            type of String
	 * @return String
	 */
	public static String addQuotes(String str) {
		String s = getStr(str);
		return "'" + s + "%'";
	}

	/**
	 * 功能: 得到一个模糊匹配的语句, 添加“%”
	 * 
	 * @param str
	 *            type of String
	 * @return String
	 */
	public static String appendQuotes(String str) {
		String s = replaceQuoted(str);
		return "" + s + "%";
	}

	/**
	 * 功能: 根据总共的记录计算出多少页数
	 * 
	 * @param size
	 *            type of int
	 * @param pageSize
	 *            type of int
	 * @return int
	 */
	public static int getPages(int size, int pageSize) {
		if (pageSize < 1)
			return 0;
		int p = (size % pageSize == 0) ? (size / pageSize)
				: ((size / pageSize) + 1);
		return p;
	}

	/**
	 * 功能: 计算字符串的长度,包括中文、英文的
	 * 
	 * @param str
	 *            type of String
	 * @return int
	 */
	public static int length(String str) {
		if (str == null)
			return 0;
		int sum = 0;
		for (int i = 0; i < str.length(); i++) {
			int ch_code = (int) str.charAt(i);
			if (ch_code < 255) {
				sum++;
			} else {
				sum += 2;
			}
		}
		return sum;
	}

	/**
	 * 功能: 把一个字符串数组中的数据加到一个字符串中，用‘,’隔开
	 * 
	 * @param params
	 *            type of an array String
	 * @return String
	 */
	public static String parseStr(String[] params) {
		StringBuffer sb = new StringBuffer();
		String str = null;
		if (params != null) {
			for (int i = 0; i < params.length; i++) {
				str = params[i];
				if (str != null && str.length() != 0) {
					sb.append(str + ",");
				}
			}
		}
		str = sb.toString();
		str = (str.endsWith(",")) ? str.substring(0, str
				.length() - 1) : str;
		return str;
	}

	/**
	 * 功能: 对字符串进行处理，只能显示不超过9个汉字的字符串，超过9个字符的，只显示前9个
	 * 
	 * @param str
	 *            type of String
	 * @return String
	 */
	public static String splitStr(String str) {
		if (str == null)
			return "";
		return ((str.length() > 9) ? str.substring(0, 9) + "..." : str);
	}

	/**
	 * 功能: 对字符串进行处理，只能显示不超过i个汉字的字符串，超过i个字符的，只显示前i个
	 * 
	 * @param str
	 *            type of String
	 * @return String
	 */
	public static String splitStr(String str, int i) {
		if (str == null)
			return "";
		return ((str.length() > i) ? str.substring(0, i) + "..." : str);
	}

	/**
	 * 功能: 返回一个不为空的字符串
	 * 
	 * @param str
	 *            type of String
	 * @return string
	 */
	public static String getStr(String str) {
		return (str == null) ? "" : str;
	}

	/**
	 * 功能: 把null转换为空字符串
	 * 
	 * @param in
	 *            type of String
	 * @return String
	 */
	public static String chgNull(String in) {
		String out = null;
		out = in;
		if (out == null || (out.trim()).equals("null")) {
			return "";
		} else {
			return out.trim();
		}
	}

	

	/**
	 * 功能: 把数组转换成字符串，中间用制定符号分割
	 * 
	 * @param arr
	 * @param comma
	 * @return
	 */
	public static String arrToStr(String[] arr, String comma) {
		if (arr == null)
			return "";
		String str = "";
		int size = arr.length;
		for (int i = 0; i < size; i++) {
			if (!chgNull(arr[i]).equals("")) 
			{
				if ( "".equals( str ) )
				{
					str =arr[i];
				}	
				else
				{
					str =str+comma + arr[i];
				}	
				
			}
		}
		return str;
	}

	/**
	 * 功能: 把数组转换成字符串，中间用逗号分割
	 * 
	 * @param arr
	 * @param comma
	 * @return
	 */
	public static String arrToStr(ArrayList<?> arr, String comma) {
		String str = "";
		int size = arr.size();
		for (int i = 0; i < size; i++) {
			str += str.equals("") ? (String) arr.get(i) : comma
					+ (String) arr.get(i);
		}
		return str;
	}
	
	/**
	 * 功能: 把List转换成字符串，中间用逗号分割
	 * 
	 * @param arr
	 * @param comma
	 * @return
	 */
	public static String listToStr(List<?> arr, String comma) {
		String str = "";
		int size = arr.size();
		for (int i = 0; i < size; i++) {
			str += str.equals("") ? (String) arr.get(i) : comma
					+ (String) arr.get(i);
		}
		return str;
	}

	
	
	/**
	 * 功能: 把以制定分割符分割的字符串转换成ArrayList
	 * 
	 * @param str
	 * @param comma
	 * @return
	 */
	public static String[] strToArr(String str, String comma) {
		String rs = chgNull(str);
		if (rs.trim().length() == 0)
			return new String[0];
		return str.split(comma);
	}

	



	
	
	/**
	 * 功能: 把用逗号分割的字符串的每个元素，两头加上单引号
	 * 
	 * @param str
	 * @return
	 */
	public static String addQuotation(String str) {
		/*if (!chgNull(str).equals("")) {
			String[] strArr = StrToArr(str);
			int size = strArr.length;
			String[] tempArr = new String[size];
			for (int i = 0; i < strArr.length; i++) {
				String newstr = strArr[i];
				newstr = "'" + newstr + "'";
				tempArr[i] = newstr;
			}
			return ArrToStr(tempArr);
		} else {
			return "''";
		}*/
		return null;
	}

	/**
	 * 功能: 转换部门代码，去调末尾的"0";例如：32030600转换为320306
	 * 
	 * @param strOrgNo
	 * @return String
	 */
	public static String chgDeptId(String oldid) {
		int len_zero = 0; // 字符串中从最后位置0的个数
		String deptid = chgNull(oldid);
		if (deptid.equals("")) {
			return "";
		}
		for (int i = deptid.length() - 1; i >= 0; i--) {
			if (deptid.substring(i, i + 1).equals("0")) {
				len_zero++;
			} else {
				break;
			}
		}
		deptid = deptid.substring(0, deptid.length() - len_zero);
		if ((deptid.length() % 2) != 0)
			deptid += "0";
		return deptid;
	}

	/**
	 * 功能: 把普通的评论转化成HTML类型的字符串
	 * 
	 * @param String
	 *            str
	 * @return String
	 */
	public static String HTMLEncode(String str) {
		if (str == null)
			return "";
		str = replace(str, "<", "&gt;");
		str = replace(str, ">", "&lt;");
		str = replace(str, "\n", "<BR>");
		return str;
	}

	/**
	 * 功能: 拷贝属性值，并捕捉错误
	 * 
	 * @param dest
	 * @param orig
	 */
	public static void copyProperties(Object dest, Object orig) {
		try {
			BeanUtils.copyProperties(dest, orig);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 功能: 根据当前时间，返回字符串，作为文件名
	 * 
	 * @return
	 */
	public static String timeFileName() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String strDate = sdf.format(date);
		return strDate;
	}

	

	/**
	 * 功能: 去掉字符串的尾部0
	 * 
	 * @param string
	 *            尾部有0的字符串
	 * @return 尾部无0的字符串 add by haojun 2005-09-02
	 */
	public static String trimEndZero(String string) {
		if (string == null)
			return "";
		int len = string.length() - 1;
		for (int i = len; i >= 0; i--) {
			if (string.charAt(i) == '0')
				string = string.substring(0, i);
			else
				break;
		}
		string = string.trim();
		return string;
	}

	/**
	 * 功能: 字符编码
	 * 
	 * @param in
	 * @return
	 */
	public static String coding(String in) {
		String out = null;
		out = Utils.chgNull(in);
		try {
			out = new String(out.getBytes("iso8859_1"), "gbk");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return out;
	}

	/**
	 * 功能: 返回指定日期为星期几
	 * 
	 * @param date
	 * @return
	 */
	public static int getWeekInt(Date date) {
		if (date == null)
			return -1;
		Calendar cetemp = Calendar.getInstance();
		cetemp.setTime(date);
		return cetemp.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 功能: 返回当前月份有几周
	 * 
	 * @param monthEndDate
	 * @return
	 */
	public static int getWeeksInMonth(String monthEndDate) {
		if (monthEndDate == null || monthEndDate.equals(""))
			return -1;
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = dateFormat.parse(monthEndDate);
			Calendar cstemp = Calendar.getInstance();
			cstemp.setTime(date);
			return cstemp.get(Calendar.WEEK_OF_MONTH);
		} catch (ParseException e) {
			e.printStackTrace();
			return -1;
		}

	}

	/**
	 * 功能: 返回当前月份有几周
	 * 
	 * @param monthEndDate
	 * @return
	 */
	public static int getWeeksInMonth(Date monthEndDate) {
		if (monthEndDate == null)
			return -1;
		Calendar cstemp = Calendar.getInstance();
		cstemp.setTime(monthEndDate);
		return cstemp.get(Calendar.WEEK_OF_MONTH);

	}

	/**
	 * 功能: 返回当前月份有几周
	 * 
	 * @param monthStartDate
	 * @return
	 */
	public static int getWeeksInMonth0(Date monthStartDate) {
		if (monthStartDate == null)
			return -1;
		Calendar cstemp = Calendar.getInstance();
		cstemp.setTime(monthStartDate);
		cstemp.add(Calendar.MONTH, 1);
		cstemp.add(Calendar.DATE, -1);
		return cstemp.get(Calendar.WEEK_OF_MONTH);

	}

	/**
	 * 功能: 返回列数
	 * 
	 * @param status
	 * @return
	 */
	public static int getCols(String status) {
		if (status == null || status.equals(""))
			return -1;
		if (status.equals("A"))
			return 2;
		if (status.equals("P"))
			return 1;
		// if (status.equals("N"))
		// return 1;
		return 0;

	}

	/**
	 * 功能: 把中文转化成相应的英文字符
	 * 
	 * @param key
	 *            type of String,
	 * @param param
	 *            type of String,要转化的字符串
	 * @return String
	 */
	public static String getDecoding(String key, String param) {
	/*	String rs = "";
		if (param == null || param.trim().equals(""))
			return rs;
		PropertiesReader reader = PropertiesReader.getIntance();
		String v = coding(reader.getProperty(key));
		param = chgNull(param);
		StringTokenizer st = new StringTokenizer(v, Constants.TOKEN_NAME);
		for (; st.hasMoreTokens();) {
			String pstr = st.nextToken();
			if (!pstr.equals("") && pstr.endsWith(param)) {
				rs = pstr.substring(0, 1);
				break;
			}
		}*/
		//return rs.trim().toUpperCase();
		return null;
	}



	

	/**
	 * 功能: 返回计量单位
	 * 
	 * @param len
	 * @return
	 */
	public static String getUnit(int len) {
		String formatsid = "";
		switch (len) {
		case 1:
			formatsid = "";
			break;
		case 2:
			formatsid = "十";
			break;
		case 3:
			formatsid = "百";
			break;
		case 4:
			formatsid = "千";
			break;
		case 5:
			formatsid = "万";
			break;
		case 6:
			formatsid = "十万";
			break;
		case 7:
			formatsid = "百万";
			break;
		case 8:
			formatsid = "千万";
			break;
		case 9:
			formatsid = "亿";
			break;
		case 10:
			formatsid = "十亿";
			break;

		}
		return formatsid;
	}

	/**
	 * 功能: 转换为中文数字
	 * 
	 * @param number
	 * @return
	 */
	public static String strToChinese(String number) {
		int intNum = Integer.parseInt(number);
		String formatsid = "";
		switch (intNum) {
		case 1:
			formatsid = "一";
			break;
		case 2:
			formatsid = "二";
			break;
		case 3:
			formatsid = "三";
			break;
		case 4:
			formatsid = "四";
			break;
		case 5:
			formatsid = "五";
			break;
		case 6:
			formatsid = "六";
			break;
		case 7:
			formatsid = "七";
			break;
		case 8:
			formatsid = "八";
			break;
		case 9:
			formatsid = "九";
			break;
		case 10:
			formatsid = "十";
			break;
		default:
			formatsid = "";
			break;
		}
		return formatsid;

	}

	
	
	/**
	 * 功能: 复制流 is -> out
	 * 
	 * @param is
	 * @param out
	 */
	public static void copyStream(InputStream is, OutputStream out) {
		if (is == null || out == null) {
			return;
		}
		try {
			int bytesRead = 0;
			byte[] buffer = new byte[8192];
			while ((bytesRead = is.read(buffer,0,8192)) != -1) {
				out.write(buffer, 0, bytesRead);
				out.flush();
			}
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			try {
				if(out!=null)
					out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}

	/**
	 * 功能: 格式化整数，如: 123-->000000123
	 * 
	 * @param i
	 * @param len
	 * @return
	 */
	public static String formatInteger(int i, int len) {
		String fmt = "";
		for (int j = 1; j <= len; j++) {
			fmt = fmt + "0";
		}
		DecimalFormat df = new DecimalFormat(fmt);
		return df.format(i);
	}
	
	

	public static String getWeekShortString(int weekday) {

		String dtmpStr = "";
		int i = weekday - 1;
		switch (i) {
		case 0:
			dtmpStr += "日";
			break;
		case 1:
			dtmpStr += "一";
			break;
		case 2:
			dtmpStr += "二";
			break;
		case 3:
			dtmpStr += "三";
			break;
		case 4:
			dtmpStr += "四";
			break;
		case 5:
			dtmpStr += "五";
			break;
		case 6:
			dtmpStr += "六";
			break;
		}
		return dtmpStr;

	}

	public static String getWeekAllString(int weekday) {

		String dtmpStr = "";
		int i = weekday - 1;
		switch (i) {
		case 0:
			dtmpStr += "星期日";
			break;
		case 1:
			dtmpStr += "星期一";
			break;
		case 2:
			dtmpStr += "星期二";
			break;
		case 3:
			dtmpStr += "星期三";
			break;
		case 4:
			dtmpStr += "星期四";
			break;
		case 5:
			dtmpStr += "星期五";
			break;
		case 6:
			dtmpStr += "星期六";
			break;
		}
		return dtmpStr;

	}

	/**
	 * 功能: 返回列数
	 * 
	 * @param status
	 * @return
	 */
	public static String getStatusstr(String status) {
		// 去掉晚上
		if (status == null || status.equals(""))
			return "";
		if (status.equals("A"))
			return "上午";
		if (status.equals("P"))
			return "下午";
		if (status.equals("N"))
			return "晚上";
		return "";

	}

}
